﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlertSystem.Business
{
    public partial class Warning_Method_Type
    {
        #region Singleton Pattern
        private static object _lock = new object();
        private static List<Warning_Method_Type> _instance;
        public static List<Warning_Method_Type> GetInstance()
        {
            lock (_lock)
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        using (var context = new DBDataContext(AppSetting.ConnectionString))
                        {
                            _instance = context.Warning_Method_Types.ToList();
                        }
                    }
                }
            }
            return _instance;
        }
        public static void ReloadData()
        {
            lock (_lock)
            {
                using (var context = new DBDataContext(AppSetting.ConnectionString))
                {
                    _instance = context.Warning_Method_Types.ToList();
                }
            }
        }
        #endregion Singleton Pattern

        #region static method
        public static Warning_Method_Type GetByID(string Id)
        {
            try
            {
                return GetInstance().Find(p => p.Method_Type == Id);
            }
            catch
            {
                return null;
            }
        }

        public static Warning_Method_Type FindItem(Predicate<Warning_Method_Type> predicate)
        {
            try
            {
                return GetInstance().Find(predicate);
            }
            catch
            {
                return null;
            }
        }

        public static List<Warning_Method_Type> Paging(DBDataContext context, int currentPage, int pageSize, ref int totalRecord, string system_id, string description)
        {
            var resutl = context.Warning_Method_Types.AsQueryable();
            if (!string.IsNullOrWhiteSpace(system_id))
            {
                resutl = resutl.Where(p => p.Method_Type.Contains(system_id));
            }
            if (!string.IsNullOrWhiteSpace(description))
            {
                resutl = resutl.Where(p => p.Method_Description.Contains(description));
            }
            totalRecord = resutl.Count();
            return resutl.Skip(((currentPage - 1) < 0 ? 0 : (currentPage - 1)) * pageSize).Take(pageSize).ToList();
        }
        #endregion static method
    }
}
